//
//  ShowPriceBaseCell.swift
//  ZYA
//
//  Created by 邱仙凯 on 2024/9/20.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
import Kingfisher
class ShowPriceBaseCell: UITableViewCell {
    
    
    lazy var listView: UITableView = {
        let list = UITableView(frame: .zero, style: .plain)
        list.dataSource = self
        list.delegate = self
        list.register(ShowPriceBaseCellSubCell.classForCoder(), forCellReuseIdentifier: ShowPriceBaseCellSubCell.identifier)
        list.tableFooterView = UIView()
        list.isScrollEnabled = false
        list.separatorStyle = .none
        list.extClipsToBounds(bounds: true)
        return list
    }()
    
    
    lazy var btn1: StatusButton = {
        let btn1 = StatusButton()
        return btn1
    }()
    lazy var lab1: UILabel = {
        let lab1 = UILabel()
        lab1.numberOfLines = 2
        lab1.font = UIFont.systemFont(ofSize: 16)

        return lab1
    }()

    
    lazy var mychujiaBtn:UIButton = {
        var v  = UIButton()
        v.setTitle("出价明细", for: .normal)
        v.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        v.setTitleColor(UIColor(kshex: tenderBlue), for: .normal)
        return v
    }()
    lazy var lineV:UIView = {
       var v = UIView()
        v.backgroundColor = UIColor(kshex: tenderBlue)
        return v
    }()
    
    
    lazy var oper_btn: OperBtn = {
        let oper_btn = OperBtn()
        oper_btn.contentMode =  .scaleAspectFit
        oper_btn.imageView?.contentMode = .scaleAspectFit
        oper_btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        oper_btn.setTitleColor(#colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1), for: .normal)
        return oper_btn
    }()
    lazy  var imageV:UIImageView = {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFit
        imageV.layer.masksToBounds = true
        imageV.layer.cornerRadius = 8
        return imageV
    }()
    
    
    var btn2Closure:((UIButton,String)->())?
    var viewJPClosure:((UIButton,String)->())?
    lazy var titleAttributes:[NSAttributedString.Key : NSObject] = {
        let paraph = NSMutableParagraphStyle()
        //将行间距设置为
        paraph.lineSpacing = 9
        //样式属性集合
        let titleAttributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 17),
                               NSAttributedString.Key.paragraphStyle: paraph]
        return titleAttributes
    }()
    lazy var lab3Attributes:[NSAttributedString.Key : NSObject] = {
        
        
        let paraph2 = NSMutableParagraphStyle()
        //将行间距设置为
        paraph2.lineSpacing = 6
        //样式属性集合
        let  lab3Attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
                               NSAttributedString.Key.paragraphStyle: paraph2]
        return lab3Attributes
    }()
    let helper = ZyaTenderHelper()
    var cover:UIView = {
       let v = UIView()
        v.backgroundColor = .white
        return v
    }()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        self.contentView.addSubview(btn1)
        
        self.contentView.addSubview(lab1)
        
        addSubview(listView)

        self.contentView.addSubview(mychujiaBtn)
        self.contentView.addSubview(lineV)
         
        
        self.contentView.addSubview(oper_btn)
        
        self.contentView.addSubview(imageV)
        self.contentView.addSubview(cover)

        
    }
    
    
    func makeConstrainsts() {
        
        cover.snp.makeConstraints { make in
            make.height.equalTo(btn1)
            make.left.equalTo(btn1)
            make.width.equalTo(8)
            make.centerY.equalTo(btn1)
        }
        btn1.snp.makeConstraints { (make) in
            make.left.equalTo(lab1).offset(-8)
            make.width.equalTo(70)
            make.height.equalTo(25)
            make.top.equalTo(lab1)
        }
        lab1.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView).offset(11)
            make.right.equalTo(self.contentView).offset(-65)
            make.top.equalTo(self.contentView).offset(6)
            make.height.equalTo(20)
        }
        
        
        listView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(lab1.snp.bottom).offset(6)
            make.bottom.equalToSuperview().offset(-5)
        }

        
        oper_btn.snp.makeConstraints { (make) in
            // 和最后一个lab平齐，内部撑起cell高度
            make.width.equalTo(70)
            make.right.equalTo(self.contentView).offset(-1)
            make.height.equalTo(32)
            make.bottom.equalTo(self.contentView).offset(-5)
            
        }
        
        mychujiaBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            make.centerY.equalTo(btn1)
            make.width.equalTo(58)
            make.height.equalTo(17)
        }
        lineV.snp.makeConstraints { make in
            make.width.equalTo(mychujiaBtn)
            make.height.equalTo(1)
            make.top.equalTo(mychujiaBtn.snp.bottom)
            make.centerX.equalTo(mychujiaBtn)
        }
        
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var one = ZtbContentModel(JSON:["":""])! {
        didSet {
            
           
            
            showData()
     
        }
    }
    
    func calculateHeight(one: ZtbContentModel) -> CGFloat {
        
        let lab = UILabel().extNumberOfLines(lines: 2)
            .extFrameSize(size: CGSize(width: KS_SCREEN_WIDTH - (11 + 65), height: 20))
        lab.attributedText = NSAttributedString(string: "                \(one.zTbCompany.unwrap)", attributes: titleAttributes)
        lab.sizeToFit()
        
        return 6 + lab.ks_height + 6 + CGFloat(one.bidInfoArr?.count ?? 0)*30.0 + 10 + (one.zStatus.unwrap == "1" ? 42 : 1)
        
    }
    
    func showData() {
        lab1.attributedText = NSAttributedString(string: "                \(one.zTbCompany.unwrap)", attributes: titleAttributes)
        let labH = lab1.sizeThatFits(CGSize(width: KS_SCREEN_WIDTH - (11 + 65), height: 50)).height
        lab1.snp.remakeConstraints { make in
            make.left.equalTo(self.contentView).offset(11)
            make.right.equalTo(self.contentView).offset(-65)
            make.top.equalTo(self.contentView).offset(6)
            make.height.equalTo(max(labH, 25))
        }
        
   
        listView.reloadData()
        listView.snp.updateConstraints { make in
            make.bottom.equalToSuperview().offset(one.zStatus.unwrap == "1" ? -42 : -5)
        }
        
        
        if one.zStatus.unwrap == "1" {
            //                btnName =  "已成交"
            oper_btn.setTitle("车辆信息", for: .normal)
            
        } else if one.zStatus.unwrap == "0" {
            //                btnName =  "未成交"
            oper_btn.setTitle("", for: .normal)
            
        } else {
            //                btnName =  "已弃拍"
            oper_btn.setTitle("", for: .normal)
        }
        
        btn1.setZBJPStatus(listType:.TB,auditmark: intTran(one.zAuditMark), status: intTran(one.zStatus),tbType: one.innerType!)
    }
    
    @objc func btn2Action(sender:UIButton) {
        if let closure = btn2Closure {
            closure(sender,sender.titleLabel!.text.unwrap)
        }
    }
    
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}



extension ShowPriceBaseCell: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return one.bidInfoArr?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ShowPriceBaseCellSubCell.identifier) as! ShowPriceBaseCellSubCell
        
        let model = one.bidInfoArr![indexPath.row]
        cell.model = model
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 30
    }
    
}




final class ShowPriceBaseCellSubCell: UITableViewCell {
    //MARK: Public
    var model: (String, String)? {
        didSet {
            firstLab.extText(text: model?.0)
            lastLab.extText(text: model?.1)
            
            let firstW = max(firstLab.sizeThatFits(CGSize(width: 200, height: 20)).width, 70)
            firstLab.snp.updateConstraints { make in
                make.width.equalTo(firstW + 1)
            }
        }
    }
    
    private let firstLab = UILabel().easy(UIColor(kshex: "666666"), .systemFont(ofSize: 15))
    
    private let lastLab = UILabel().easy(UIColor(kshex: "666666"), .systemFont(ofSize: 15))
    
    //MARK: UI
    func initUI() {
        addSubview(firstLab)
        firstLab.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.bottom.equalToSuperview()
            make.width.equalTo(10)
        }
        
        addSubview(lastLab)
        lastLab.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-10)
            make.top.bottom.equalToSuperview()
            make.left.equalTo(firstLab.snp.right).offset(20)
        }
    }
    
    //MARK:  Override
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {}
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {}
    
}
